package test.com.sms.selenium.sms.resourcemanagement.util;
import static test.com.sms.selenium.sms.resourcemanagement.util.RmResourceManagementPageUtil.RM_BUTTON_ADVANCED_SEARCH;

import org.openqa.selenium.By;
import org.openqa.selenium.support.ui.Select;

import test.com.sms.selenium.framework.SeleniumPageUtil;
import test.com.sms.selenium.util.SeleniumUtil;

public class RmResourceManagementAdvancedSearchPageUtil extends SeleniumPageUtil {
	public final static By SEARCH_INPUT_FIRST_NAME = By.id("firstName");
	public final static By SEARCH_INPUT_LAST_NAME = By.id("lastName");
	public final static By SEARCH_INPUT_COMPANY_NAME = By.id("companyName");
	public final static By SEARCH_INPUT_ALT_ID = By.id("alternateId");
	public final static By SEARCH_INPUT_SERVICE_RADIUS = By.id("serviceRadius");
	public final static By SEARCH_INPUT_SKILL_NAME_1 = By.id("skillName1");
	public final static By SEARCH_INPUT_SKILL_NAME_2 = By.id("skillName2");
	public final static By SEARCH_INPUT_SKILL_NAME_3 = By.id("skillName3");
	public final static By SEARCH_SELECT_SKILL_SET_GROUP = By.id("skillSetGroupIds");
	public final static By SEARCH_SELECT_WAREHOUSE = By.id("warehouseIds");
	public final static By SEARCH_SELECT_REGION = By.id("regionIds");
	public final static By SEARCH_SELECT_DISTRICT = By.id("districtIds");
	public final static By SEARCH_SELECT_RESOURCE_TYPE = By.id("resourceTypeCodes");
	public final static By SEARCH_SELECT_DISPATCH_PREF = By.id("dispatchingTypeCodes");
	public final static By SEARCH_SELECT_COUNTRY_1 = By.id("countryCode1");
	public final static By SEARCH_INPUT_ADDRESS_1 = By.id("address1");
	public final static By SEARCH_INPUT_CITY_1 = By.id("city1");
	public final static By SEARCH_INPUT_ZIP_1 = By.id("zip1");
	public final static By SEARCH_SELECT_STATE_CODE_1 = By.id("stateCode1");
	public final static By SEARCH_SELECT_COUNTRY_2 = By.id("countryCode2");
	public final static By SEARCH_INPUT_ADDRESS_2 = By.id("address2");
	public final static By SEARCH_INPUT_CITY_2 = By.id("city2");
	public final static By SEARCH_INPUT_ZIP_2 = By.id("zip2");
	public final static By SEARCH_SELECT_STATE_CODE_2 = By.id("stateCode2");
	public final static By SEARCH_SELECT_COUNTRY_3 = By.id("countryCode3");
	public final static By SEARCH_INPUT_ADDRESS_3 = By.id("address3");
	public final static By SEARCH_INPUT_CITY_3 = By.id("city3");
	public final static By SEARCH_INPUT_ZIP_3 = By.id("zip3");
	public final static By SEARCH_SELECT_STATE_CODE_3 = By.id("stateCode3");
	
	// advanced search window
	public final static By SEARCH_DIV_ADVANCED_SEARCH = By.id("searchMenu");
	public final static By SEARCH_BUTTON_SEARCH = By.id("searchSubmit");
	public final static By SEARCH_BUTTON_CLEAR = By.xpath(".//*[@id='resourceSearch']/input[1]");
	
	// elements of ticket list
	public final static By SEARCH_TD_NAMES = By.xpath("//*[@id='table-data']/tbody/tr/td[2]");
	
	
	public final static void waitForAdvancedSearchPageLoad(){
		SeleniumUtil.waitOnElements(new By[]{
				SEARCH_SELECT_COUNTRY_1, SEARCH_INPUT_CITY_1, SEARCH_SELECT_STATE_CODE_1 
		});
		SeleniumUtil.assertTrue(SeleniumUtil.isDisplayed(SeleniumUtil.getCachedElement(SEARCH_SELECT_COUNTRY_1)));
		SeleniumUtil.assertTrue(SeleniumUtil.isDisplayed(SeleniumUtil.getCachedElement(SEARCH_INPUT_CITY_1)));
		SeleniumUtil.assertTrue(SeleniumUtil.isDisplayed(SeleniumUtil.getCachedElement(SEARCH_SELECT_STATE_CODE_1)));
	}

	/**
	 * Clear current search criteria
	 * */
	public static void clear() {
		SeleniumUtil.click(SeleniumUtil.findElement(SEARCH_BUTTON_CLEAR));
		SeleniumUtil.waitForFixedMilliseconds(1000);
	}
	
	/**
	 * Search by resource type
	 * @param resource type
	 * */
	public final static void searchByResourceType(String resType){
		SeleniumUtil.waitOnElements(new By[]{RM_BUTTON_ADVANCED_SEARCH});
		SeleniumUtil.click(SeleniumUtil.findElement(RM_BUTTON_ADVANCED_SEARCH));
		RmResourceManagementAdvancedSearchPageUtil.clear();
		SeleniumUtil.waitOnElements(new By[]{SEARCH_SELECT_REGION});
		SeleniumUtil.selectByVisibleText(new Select(SeleniumUtil.findElement(SEARCH_SELECT_REGION)), "All");
		SeleniumUtil.waitForFixedMilliseconds(100);
		SeleniumUtil.waitOnElements(new By[]{SEARCH_SELECT_RESOURCE_TYPE});
		SeleniumUtil.selectByVisibleText(new Select(SeleniumUtil.findElement(SEARCH_SELECT_RESOURCE_TYPE)), resType);
		
		SeleniumUtil.waitOnElements(new By[]{SEARCH_BUTTON_SEARCH});
		SeleniumUtil.click(SeleniumUtil.findElement(SEARCH_BUTTON_SEARCH));
		SeleniumUtil.waitForFixedMilliseconds(3000);
	}
	
	/**
	 *	advanced search by company name 
	 */
	public final static void searchByCompanyName(String companyName){
		SeleniumUtil.waitOnElements(new By[]{RM_BUTTON_ADVANCED_SEARCH});
		SeleniumUtil.click(SeleniumUtil.findElement(RM_BUTTON_ADVANCED_SEARCH));
		SeleniumUtil.waitOnElements(new By[]{SEARCH_SELECT_REGION});
		RmResourceManagementAdvancedSearchPageUtil.clear();
		SeleniumUtil.selectByVisibleText(new Select(SeleniumUtil.findElement(SEARCH_SELECT_REGION)), "All");
		SeleniumUtil.waitForFixedMilliseconds(100);
		SeleniumUtil.waitOnElements(new By[]{SEARCH_INPUT_COMPANY_NAME});
		SeleniumUtil.sendKeys(SeleniumUtil.getCachedElement(SEARCH_INPUT_COMPANY_NAME), companyName);
		
		SeleniumUtil.waitOnElements(new By[]{SEARCH_BUTTON_SEARCH});
		SeleniumUtil.click(SeleniumUtil.findElement(SEARCH_BUTTON_SEARCH));
		SeleniumUtil.waitForFixedMilliseconds(3000);
	}
}
